using System;
using System.Linq;
using PropertyExpression.ReactiveRelation.Test.Common;

namespace PropertyExpression.ReactiveRelation.Test
{
    internal static class BusinessFacade
    {
        public static ICalcInitiator<Quote> CreateQuote(
            DataClassesDataContext context, ICalcExecutor calcExecutor, Func<Dealer> dealerFunc)
        {
            var quote = calcExecutor.CreateAndCoupleEntity<Quote>();
            context.Quotes.InsertOnSubmit(quote.Entity);

            quote._(_ => _.QuoteId)(IdGenerator.GetId());

            var dealer = dealerFunc();

            quote._(_ => _.DealerId)(dealer.DealerId);
            quote._(_ => _.DealerContactName)(dealer.DealerCompanyName);
            quote._(_ => _.DealerName)(dealer.DealerCompanyName);
            quote._(_ => _.DealerPhone)(dealer.DealerPhone);
            quote._(_ => _.DealerFax)(dealer.DealerFax);
            quote._(_ => _.DealerAddress)(dealer.DealerAddress);
            quote._(_ => _.DealerCity)(dealer.DealerCity);
            quote._(_ => _.DealerCountry)(dealer.DealerCountry);
            quote._(_ => _.DealerState)(dealer.DealerStateProvince);
            quote._(_ => _.DealerZIPCode)(dealer.DealerZipPostalCode);

            foreach (var dealerOtherExpense in dealer.DealerOtherExpenses)
            {
                var expense = calcExecutor.CreateAndCoupleEntity<QuoteOtherExpense>();
                expense._(_ => _.QuoteOtherExpenseId)(IdGenerator.GetId());
                expense._(_ => _.Description)(dealerOtherExpense.Description);
                expense._(_ => _.Quantity)(1);
                expense._(_ => _.Fix)(dealerOtherExpense.Fix);
                expense._(_ => _.PartPercent)(dealerOtherExpense.PartPercent);
                expense._(_ => _.LaborPercent)(dealerOtherExpense.LaborPercent);
                expense._(_ => _.Quote).OverloadCall(quote);
            }

            return quote;
        }

        internal static Dealer GetDealer(DataClassesDataContext context)
        {
            return context.Dealers.First();
        }
    }
}